<template>
  <el-row type="flex" justify="space-between" align="middle" style="height: 40px; width: 100%;">
    <el-col :span="20">
      <span>{{ nodeData.name }}</span>
    </el-col>
    <el-col :span="4">
      <el-row type="flex" justify="end">
        <!-- 两个内容 -->
        <el-col>{{ nodeData.manager }}</el-col>
        <el-col>
          <!-- 下拉菜单 element -->
          <el-dropdown @command="handelAction">
            <span> 操作<i class="el-icon-arrow-down" /> </span>
            <!-- 下拉菜单 -->
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item command="add">添加子部门</el-dropdown-item>
              <template v-if="!nodeData.root">
                <el-dropdown-item command="edit">编辑部分</el-dropdown-item>
                <el-dropdown-item command="del">删除部门</el-dropdown-item>
              </template>
            </el-dropdown-menu>
          </el-dropdown>
        </el-col>
      </el-row>
    </el-col>
  </el-row>
</template>
<script>
import reqDelDepartmens from '@/api/departments'
export default {
  props: {
    nodeData: {
      type: Object,
      default: () => ({})
    }
  },
  methods: {
    async handelAction (type) {
      if (type === 'add') {
        // 添加功能,显示弹窗
        this.$emit('toggleBox', {
          isShow: true,
          type: 'add',
          pid: this.nodeData.id
        })
      } else if (type === 'edit') {
        // 编辑功能，显示弹窗
        this.$emit('toggleBox', {
          isShow: true,
          // 添加一个辨识符
          type: 'edit',
          pid: this.nodeData.id
        })
      } else if (type === 'del') {
        // 删除功能
        try {
          const ret = await this.$confirm('确认要删除部门吗？, 是否继续?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).catch(err => err)
          // 证明点击取消按钮
          if (ret === 'cancel') return
          // 点击确定按钮后，执行后续操作
          const delRet = await reqDelDepartmens(this.nodeData.id)
          if (delRet.code === 10000) {
            // 删除部门成功
            this.$message.success('删除部门成功')
            // 通知父组件刷新列表
            this.$emit('on-success')
          }
        } catch {
          this.$message.error('删除部门失败')
        }
      } else {
        // 其他情况
      }
    }
  }
}
</script>
